﻿using System.Threading.Tasks;
using VOICeVIO.Hybrid.Models;
using VOICeVIO.Hybrid.Services;

namespace VOICeVIO.Hybrid.ViewModels.CRUD
{
    public class EditViewModel : MasterPageViewModel
    {
        private readonly HybridService _hybridService;

        public SongEditModel Song { get; set; }

        public EditViewModel(HybridService hybridService)
        {
            this._hybridService = hybridService;
        }


        public override async Task PreRender()
        {
            string uid = Context.Parameters["Id"].ToString();
            Song = await _hybridService.GetSongByUidAsync(uid);
            if (Song == null)
            {
                Context.RedirectToRoutePermanent("Default");
            }
            await base.PreRender();
        }


        public async Task EditSong()
        {
            await _hybridService.UpdateSongAsync(Song);
            Context.RedirectToRoute("CRUD_Detail", new { Id = Song.Id });
        }

        public async Task DeleteSong()
        {
            await _hybridService.DeleteSongAsync(Song.Uid);
            Context.RedirectToRoutePermanent("Default", replaceInHistory: true);
        }

        public async Task DownloadSong()
        {

        }
    }
}
